<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="UTF-8" content="#">
    <title>销售订单页面</title>
    <!-- 引入CSS -->
    <link rel="stylesheet" type="text/css"
          href="/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="/easyui/css/demo.css">
    <!-- 引入JS -->
    <script type="text/javascript" src="/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/easyui/themes/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript"
            src="/easyui/js/validateExtends.js"></script>
    <script type="text/javascript" src="/common/js/common.js"></script>
    <script type="text/javascript"
            src="/common/js/order.js"></script>
    <script type="text/javascript"
            src="/common/js/status.js"></script>
    <script type="text/javascript">

        var login_name;
        //DOM加载完成后执行的回调函数
        $(function () {

            $("#add_thingName").textbox('textbox').css('backgroundColor', "#aaa");
            $("#add_unit").textbox('textbox').css('backgroundColor', "#aaa");

            //初始化日历控件
            $('#update_planTime').datebox({
                required: true,
                panelHeight: 'auto',
                formatter: function (date) {
                    var y = date.getFullYear();
                    var m = date.getMonth() + 1;
                    var d = date.getDate();
                    return y + '/' + m + '/' + d;
                },
                parser: function (s) {
                },
                editable: false
            });

            //初始化日历控件
            $('#add_planTime').datebox({
                required: true,
                panelHeight: 'auto',
                formatter: function (date) {
                    var y = date.getFullYear();
                    var m = date.getMonth() + 1;
                    var d = date.getDate();
                    return y + '/' + m + '/' + d;
                },
                parser: function (s) {
                },
                editable: false
            });

            $.post("/system/getUserName", function (data) {
                login_name = data;
            })
            var table;
            //初始化datagrid
            $('#dataList').datagrid({
                iconCls: 'icon-more',//图标
                border: true,
                collapsible: false,//是否可折叠
                fit: true,
                method: "post",
                url: "/plan-no-supplier/getPlanNoSuppliers?t" + new Date().getTime(),
                idField: 'id',
                singleSelect: false,//是否单选
                rownumbers: true,//行号
                pagination: true,//分页控件
                remoteSort: false,
                columns: [
                    [
                        {field: 'chk', checkbox: true, width: 50},
                        {field: 'id', title: 'ID', width: 35, sortable: true},
                        {
                            field: 'status', title: '物料状态', width: 90, sortable: true,
                            formatter: function (value, row, index) {
                                return getPlanNoSupplierStatus(value);
                            }
                        },
                        {field: 'supplierCode', title: '供应商编码', width: 120, sortable: true},
                        {field: 'supplierName', title: '供应商名称', width: 150, sortable: true},
                        {field: 'thingCode', title: '物料编码', width: 150, sortable: true},
                        {field: 'thingName', title: '物料名字', width: 200, sortable: true},
                        {field: 'needCnt', title: '需求量', width: 70, sortable: true},
                        {field: 'planCnt', title: '计划数量', width: 70, sortable: true},
                        {field: 'unit', title: '单位', width: 40},
                        {
                            field: 'needTime', title: '需求交期', width: 220, sortable: true,
                            formatter: function (value, row, index) {
                                return transTime(value);
                            }
                        },
                        {
                            field: 'planTime', title: '计划时间', width: 220, sortable: true,
                            formatter: function (value, row, index) {
                                return transTime(value);
                            }
                        },
                        {
                            field: 'purGroup', title: '采购组', width: 150,
                        },
                        {
                            field: 'saleOrderId', title: '销售订单编码', width: 150, sortable: true
                        }, {
                        field: 'saleOrderRow', title: '销售订单行号', width: 150, sortable: true
                    }, {
                        field: 'remark', title: '备注', width: 150, sortable: true
                    }
                    ]
                ],
                toolbar: "#toolbar"//工具栏
            });

            //设置分页控件
            var p = $('#dataList').datagrid('getPager');
            $(p).pagination({
                pageSize: 10,//设置每页显示的记录条数,默认为10
                pageList: [10, 20, 30, 50, 100],//设置每页记录的条数
                beforePageText: '第',
                afterPageText: '页    共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
            });

            //信息修改按钮事件
            $("#delete").click(function () {
                var selectRows = $("#dataList").datagrid("getSelections");//返回所有选中的行,当没有选中的记录时,将返回空数组
                var selectLength = selectRows.length;
                if (selectLength === 0) {
                    $.messager.alert("消息提醒", "请选择想要删除的数据哟!", "warning");
                } else {
                    var ids = [];
                    $(selectRows).each(function (i, row) {
                        ids[i] = row.id;//将预删除行的id存储到数组中
                    });
                    $.messager.confirm("消息提醒", "删除后将无法恢复信息! 确定继续?", function (r) {
                        if (r) {
                            $.ajax({
                                type: "post",
                                url: "/plan-no-supplier/deletePlanNoSuppliers?t" + new Date().getTime(),
                                data: {ids: ids},
                                dataType: 'json',
                                success: function (data) {
                                    if (data.success) {
                                        $.messager.alert("消息提醒", "删除成功啦!", "info");
                                        $("#dataList").datagrid("reload");//刷新表格
                                        $("#dataList").datagrid("uncheckAll");//取消勾选当前页所有的行
                                    } else {
                                        $.messager.alert("消息提醒", "服务器端发生异常! 删除失败!", "warning");
                                    }
                                }
                            });
                        }
                    });
                }
            });

            //信息添加按钮事件
            $("#add").click(function () {
                table = $("#addTable");
                $("#addTable").form("clear");//清空表单数据
                $("#addDialog").dialog("open");//打开添加窗口
            });

            $("#addDialog").dialog({
                title: "添加供应链询价报表信息窗口",
                width: 880,
                height: 530,
                iconCls: "icon-house",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                buttons: [
                    {
                        text: '添加',
                        plain: true,
                        iconCls: 'icon-add',
                        handler: function () {
                            var validate = $("#addForm").form("validate");
                            var validate = true;
                            if (!validate) {
                                $.messager.alert("消息提醒", "请检查你输入的数据哟!", "warning");
                            } else {
                                var data = $("#addForm").serialize();//序列化表单信息
                                $.ajax({
                                    type: "post",
                                    url: "/plan-no-supplier/addPlanNoSupplier?t" + new Date().getTime(),
                                    data: data,
                                    dataType: 'json',
                                    success: function (data) {
                                        if (data.success) {
                                            $("#addDialog").dialog("close"); //关闭窗口
                                            $('#dataList').datagrid("reload");//重新刷新页面数据
                                            $.messager.alert("消息提醒", "添加成功啦!", "info");
                                        } else {
                                            $.messager.alert("消息提醒", data.msg, "warning");
                                        }
                                    }
                                });
                            }
                        }
                    }, {
                        text: '关闭',
                        plain: true,
                        iconCls: 'icon-error',
                        handler: function () {
                            $("#addDialog").dialog("close");//关闭添加窗口
                        }
                    }
                ]
            });


            //信息修改按钮事件
            $("#update").click(function () {
                var selectRows = $("#dataList").datagrid("getSelections");
                if (selectRows.length !== 1) {
                    $.messager.alert("消息提醒", "请单条选择想要修改的数据哟!", "warning");
                } else {
                    if (selectRows[0].status != 0) {
                        $.messager.alert("消息提醒", getPlanNoSupplierStatus(selectRows[0].status)+"，无法修改!", "warning");
                        return false;
                    }
                    $("#updateDialog").dialog("open");
                }
            });

            //设置编辑销售信息窗口
            $("#updateDialog").dialog({
                title: "修改订单信息窗口",
                width: 660,
                height: 500,
                iconCls: "icon-house",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                buttons: [
                    {
                        text: '提交',
                        plain: true,
                        iconCls: 'icon-update',
                        handler: function () {
                            var selectRow = $("#dataList").datagrid("getSelected");
                            var planTime = $("#update_planTime").textbox('getValue');
                            var planCnt = $("#update_planCnt").textbox('getValue');
                            var id=selectRow.id
                            $.post("/plan-no-supplier/updatePlanNoSupplier",{id:id,planTime:planTime,planCnt:planCnt},function (data) {
                                if (data != null) {
                                    $.messager.alert("消息提醒", "更新成功!", "info");
                                    $("#addDialog").dialog("close"); //关闭窗口
                                    $('#dataList').datagrid("reload");//重新刷新页面数据
                                } else {
                                    $.messager.alert("消息提醒", "更新失败", "warning");
                                }
                            })
                            $("#updateTable").form("clear");//清空表单数据
                            $("#updateDialog").dialog("close");//关闭更新窗口
                        }
                    }, {
                        text: '关闭',
                        plain: true,
                        iconCls: 'icon-error',
                        handler: function () {
                            $("#updateDialog").dialog("close");//关闭添加窗口
                        }
                    }
                ],
                //打开窗口前先初始化表单数据(表单回显)
                onBeforeOpen: function () {
                    var selectRow = $("#dataList").datagrid("getSelected");
                    $("#update_id").val(selectRow.id);//初始化id值,需根据id更新采购员信息
                    $("#update_planTime").textbox('setValue', selectRow.planTime);
                    $("#update_planCnt").textbox('setValue', selectRow.planCnt);
                }
            });

            //交给采购秘书，填写采购组
            $("#confirm_purGroup").click(function () {
                var selectRows = $("#dataList").datagrid("getSelections");
                var ids = [];
                $(selectRows).each(function (i, row) {
                    if (row.status != 0) {
                        $.messager.alert("消息提醒", getPlanNoSupplierStatus(row.status) + ",无法定采购组", "warning");
                        return false;
                    }
                    var planTime = row.planTime;
                    var planCnt = row.planCnt;
                    if (planTime == "" || planTime == null) {
                        $.messager.alert("消息提醒", "请填写计划交货时间", "warning");
                        return false;
                    }
                    if (planCnt == "" || planCnt == null) {
                        $.messager.alert("消息提醒", "请填写计划数量", "warning");
                        return false;
                    }
                    ids[i] = row.id;//将预添加行的id存储到数组中
                });

                $.post("/plan-no-supplier/addPlanNoSupplierToConfirmPurGroup", {ids: ids}, function (data) {
                    if (data != null) {
                        $.messager.alert("消息提醒", "采购秘书开始确认采购组!", "info");
                        $("#dataList").datagrid("reload");//刷新表格
                        $("#dataList").datagrid("uncheckAll");//取消勾选当前页所有的行
                    } else {
                        $.messager.alert("消息提醒", "确认采购组失败", "warning");
                    }
                })
            })

            //交给采购，确认供应商
            $("#confirm_supplier").click(function () {
                var selectRows = $("#dataList").datagrid("getSelections");
                var ids = [];
                $(selectRows).each(function (i, row) {
                    if (row.status != 2) {
                        $.messager.alert("消息提醒", getPlanNoSupplierStatus(row.status) + ",无法定供应商", "warning");
                        return false;
                    }
                    ids[i] = row.id;//将预添加行的id存储到数组中
                });

                $.post("/plan-no-supplier/addPlanNoSupplierToConfirmSupplier", {ids: ids}, function (data) {
                    if (data != null) {
                        $.messager.alert("消息提醒", "采购开始确认供应商!", "info");
                        $("#dataList").datagrid("reload");//刷新表格
                        $("#dataList").datagrid("uncheckAll");//取消勾选当前页所有的行
                    } else {
                        $.messager.alert("消息提醒", "采购开始确认供应商失败", "warning");
                    }
                })
            })

            //添加 PlanPlace 视图
            $("#add_planPlace").click(function () {
                var selectRows = $("#dataList").datagrid("getSelections");
                var ids = [];
                $(selectRows).each(function (i, row) {
                    if (row.status != 5) {
                        $.messager.alert("消息提醒", getPlanNoSupplierStatus(row.status) + ",无法再次添加", "warning");
                        return false;
                    }
                    ids[i] = row.id;//将预添加行的id存储到数组中
                });

                $.post("/plan-no-supplier/addPlanNoSupplierToPlanPlace", {ids: ids}, function (data) {
                    if (data != null) {
                        $.messager.alert("消息提醒", "添加成功啦!", "info");
                        $("#dataList").datagrid("reload");//刷新表格
                        $("#dataList").datagrid("uncheckAll");//取消勾选当前页所有的行
                    } else {
                        $.messager.alert("消息提醒", "添加到PlanPlace中失败", "warning");
                    }
                })
            })



            $("#add_thingInfo").click(function () {
                var code = $("#add_thingCode").textbox('getValue');
                if (code == "") {
                    $.messager.alert("消息提醒", "请填写物料编码", "warning");
                    return false;
                }
                $.post("/tech-thing/getTechThingByThingCode", {thingCode: code}, function (data) {
                    if (data != null) {
                        $("#add_thingName").textbox('setValue', data.thingName);
                        $("#add_unit").textbox('setValue',data.unit);
                    } else {
                        $.messager.alert("消息提醒", code + "商品不存在", "warning");
                    }
                })
            })

            //销售组名和销售名搜索按钮的监听事件(将其值返回给Controller)
            $("#search-btn").click(function () {
                var chks = $("#orderAllStatus input:checked");
                var data = "";
                for (var i = 0; i < chks.length; i++) {
                    data += chks[i].value + ",";
                }
                $('#dataList').datagrid('load', {
                    personName: $('#search-saleName').textbox('getValue'),//获取销售名称
                    personGroup: $('#search-saleGroup').combobox('getValue'),//获取销售组名称
                    orderStatus: data
                });
            });
        })
        ;
    </script>
</head>
<body>

<!-- 销售列表信息 -->
<table id="dataList" cellspacing="0" cellpadding="0"></table>

<div id="toolbar">
    <!-- 学生,班级名搜索域 -->
    <div style="margin-left: 10px;">

        <div style="float: left;"><a id="add" href="javascript:" class="easyui-linkbutton"
                                     data-options="iconCls:'icon-add',plain:true">添加询价报表</a></div>
        <div style="float: left;" class="datagrid-btn-separator"></div>


        <div style="float: left;"><a id="update" href="javascript:" class="easyui-linkbutton"
                                     data-options="iconCls:'icon-add',plain:true">修改询价报表</a></div>
        <div style="float: left;" class="datagrid-btn-separator"></div>

        <div style="float: left;"><a id="delete" href="javascript:" class="easyui-linkbutton"
                                     data-options="iconCls:'icon-add',plain:true">删除询价报表</a></div>
        <div style="float: left;" class="datagrid-btn-separator"></div>

        <div style="float: left;"><a id="confirm_purGroup" href="javascript:" class="easyui-linkbutton"
                                     data-options="iconCls:'icon-add',plain:true">定采购组</a></div>
        <div style="float: left;" class="datagrid-btn-separator"></div>

        <div style="float: left;"><a id="confirm_supplier" href="javascript:" class="easyui-linkbutton"
                                     data-options="iconCls:'icon-add',plain:true">定供应商</a></div>
        <div style="float: left;" class="datagrid-btn-separator"></div>

        <div style="float: left;"><a id="add_planPlace" href="javascript:" class="easyui-linkbutton"
                                     data-options="iconCls:'icon-add',plain:true">添加到下单视图</a></div>
        <div style="float: left;" class="datagrid-btn-separator"></div>

        <!-- 商品名称搜索框 -->
        <a href="javascript:" class="easyui-linkbutton"
           data-options="iconCls:'icon-user-student',plain:true">销售组</a>
        <select id="search-techGroup" class="easyui-combobox" style="width: 250px">
        </select>

        <a href="javascript:" class="easyui-linkbutton"
           data-options="iconCls:'icon-user-student',plain:true">订单编号</a>
        <input id="search-itemCode" class="easyui-textbox"/>

        <!-- 搜索按钮 -->
        <a id="search-btn" href="javascript:" class="easyui-linkbutton"
           data-options="iconCls:'icon-search',plain:true">搜索</a>
    </div>

</div>


<div id="addDialog" style="padding: 15px 0 0 55px;">
    <!-- 供应链询价报表信息表单 -->
    <form id="addForm" method="post" action="#">
        <table id="addTable" style="border-collapse:separate; border-spacing:0 3px;" cellpadding="6">
            <tr>
                <td>物料编码</td>
                <td colspan="1">
                    <input id="add_thingCode" style="width: 200px; height: 30px;" class="easyui-textbox"
                           name="thingCode" data-options="required:true,missingMessage:'请填写物料编码哟~'"/>
                </td>
                <td>
                    <a id="add_thingInfo" href="javascript:" class="easyui-linkbutton"
                       data-options="iconCls:'icon-search',plain:true">搜索</a></td>
            </tr>

            <tr>
                <td>物料名称</td>
                <td colspan="1">
                    <input id="add_thingName" style="width: 200px; height: 30px;" class="easyui-textbox"
                           name="thingName" data-options="readonly:true,required:true,missingMessage:'请填写物料名称哟~'"/>
                </td>
            </tr>

            <tr>
                <td>使用数量</td>
                <td colspan="1">
                    <input id="add_planCnt" style="width: 200px; height: 30px;" class="easyui-textbox"
                           name="planCnt" data-options="required:true,missingMessage:'请填写计划数量哟~'"/>
                </td>
            </tr>

            <tr>
                <td>单位</td>
                <td colspan="1">
                    <input id="add_unit" style="width: 200px; height: 30px;" class="easyui-textbox"
                           name="unit" data-options="readonly:true"/>
                </td>
            </tr>

            <tr>
                <td>计划时间</td>
                <td>
                    <input id="add_planTime" style="width: 200px; height: 30px;" type="text"
                           name="planTime" data-options="required:true,missingMessage:'请填写计划时间哟~'"/>
                </td>
            </tr>

        </table>
    </form>
</div>





<div id="updateDialog" style="padding: 20px 0 0 65px">
    <!-- 学生信息表单 -->
    <form id="updateForm" method="post" action="#">
        <!-- 获取被修改信息的学生id -->
        <input type="hidden" id="update_id" name="id"/>
        <table id="updateTable" style="border-collapse:separate; border-spacing:0 3px;" cellpadding="6">

            <tr>
                <td>计划交货时间</td>
                <td colspan="4"><input id="update_planTime" style="width: 200px; height: 30px;"
                                       class="easyui-textbox"
                                       type="text" name="planTime" data-options="required:true"/>
                </td>
            </tr>

            <tr>
                <td>计划交货数量</td>
                <td colspan="4"><input id="update_planCnt" style="width: 200px; height: 30px;"
                                       class="easyui-textbox"
                                       type="text" name="planCnt" data-options="required:true"/>
                </td>
            </tr>
        </table>
    </form>
</div>


</body>
</html>